<?php
namespace app\api\model;


use think\Model;

class Address extends Common
{

	public function getNameById($id)
	{

		$province = (new Province)->field('name')->where('id',$id)->find();
		return $province->getAttr('name');
	}

	public function createOrUpdateAddress(?Address $addressObj ,$userId,$consignee,$mobile,$province,$city,$area,$address,$isDefault){
		if(empty($addressObj)){
			$addressObj = new Address;
		}
		if($isDefault){
			// 设置为默认地址
			$this->where('user_id',$userId)->update(['is_default'=>0]);
		}
		$addressObj->user_id = $userId;
		$addressObj->consignee = $consignee;
		$addressObj->mobile = $mobile;
		$addressObj->province = $province;
		$addressObj->city = $city;
		$addressObj->area = $area;
		$addressObj->address = $address;
		$addressObj->is_default = $isDefault;



		$addressObj->save();
		return $addressObj;
	}
}